package com.achilles.widget;

import android.content.Context;
import android.support.design.widget.FloatingActionButton;
import android.util.AttributeSet;
import android.util.Log;
import android.view.animation.Animation;
import android.view.animation.ScaleAnimation;
import android.view.animation.TranslateAnimation;

/**
 * Created by AchillesL on 2016-9-21.
 */
public class TaskFloatingActionButton extends FloatingActionButton implements Animation
        .AnimationListener {

    private boolean isBeginAnimation = false;
    private ScaleAnimation mScaleAnimationIn, mScaleAnimationOut;
    private TranslateAnimation mTranslateAnimationIn, mTranslateAnimationOut;

    private Mode mMode;

    private enum Mode {
        ScaleOut, ScaleIn, MoveIn, MoveOut
    }

    public TaskFloatingActionButton(Context context) {
        this(context, null);
    }

    public TaskFloatingActionButton(Context context, AttributeSet attrs) {
        this(context, attrs, 0);
    }

    public TaskFloatingActionButton(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
    }

    @Override
    protected void onSizeChanged(int w, int h, int oldw, int oldh) {
        super.onSizeChanged(w, h, oldw, oldh);

        mScaleAnimationOut = new ScaleAnimation(1f, 0f, 1f, 0f, w / 2, h / 2);
        mScaleAnimationOut.setDuration(80);
        mScaleAnimationOut.setFillAfter(true);
        mScaleAnimationOut.setAnimationListener(this);

        mScaleAnimationIn = new ScaleAnimation(0f, 1f, 0f, 1f, w / 2, h / 2);
        mScaleAnimationIn.setDuration(80);
        mScaleAnimationIn.setFillAfter(true);
        mScaleAnimationIn.setAnimationListener(this);

        mTranslateAnimationIn = new TranslateAnimation(0, 0, 400, 0);
        mTranslateAnimationIn.setDuration(200);
        mTranslateAnimationIn.setFillAfter(true);
        mTranslateAnimationIn.setAnimationListener(this);

        mTranslateAnimationOut = new TranslateAnimation(0, 0, 0, 400);
        mTranslateAnimationOut.setDuration(200);
        mTranslateAnimationOut.setFillAfter(true);
        mTranslateAnimationOut.setAnimationListener(this);

    }

    public void moveIn() {
        mMode = Mode.MoveIn;
        if (getVisibility() == VISIBLE) return;
        if (isBeginAnimation) return;
        this.startAnimation(mTranslateAnimationIn);
    }

    public void moveOut() {
        mMode = Mode.MoveOut;
        if (getVisibility() == GONE) return;
        if (isBeginAnimation) return;
        this.startAnimation(mTranslateAnimationOut);
    }

    public void scaleIn() {
        mMode = Mode.ScaleIn;
        if (getVisibility() == VISIBLE) return;
        this.setAnimation(mScaleAnimationIn);
        mScaleAnimationIn.start();
    }

    public void scaleOut() {
        mMode = Mode.ScaleOut;
        if (getVisibility() == GONE) return;
        this.setAnimation(mScaleAnimationOut);
        mScaleAnimationOut.start();
    }

    @Override
    public void onAnimationStart(Animation animation) {
        isBeginAnimation = true;
        if (mMode == Mode.MoveIn) {
            this.setVisibility(VISIBLE);
        }
    }

    @Override
    public void onAnimationEnd(Animation animation) {
        isBeginAnimation = false;
        Log.d("onAnimationEnd",mMode.name());
        this.clearAnimation();
        if (mMode == Mode.ScaleIn || mMode == Mode.MoveIn) {
            this.setVisibility(VISIBLE);
        } else if (mMode == Mode.ScaleOut || mMode == Mode.MoveOut) {
            this.setVisibility(GONE);
        }
    }

    @Override
    public void onAnimationRepeat(Animation animation) {

    }
}
